﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;
using System.Data.Common;
using DataAccess;
using System.Data.SqlClient;

public partial class Test : System.Web.UI.Page
{
    //Khai báo khoản thời gian đếm
    private TimeSpan counttime = new TimeSpan(0, 1, 0); // khoảng thời gian đếm là 0 giờ, 1 phút, 0 giây
    private static string starttime = string.Empty;

    protected void Button1_Click(object sender, EventArgs e)
    {
        starttime = DateTime.Now.ToString();
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        Session.Add("Mark", 0);
        Session.Add("CountAnserTrue", 0);
        //get time start
        Session.Add("StartTime", DateTime.Now.ToString());

        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       if (Session["Username"] == null || Session["Exam"] == null )
            Response.Redirect("Default.aspx");

       if (Session["Step1"] != null)
       {
           Response.Redirect("Step2.aspx");
       }

       if (!Page.IsPostBack)
       {
           //GET QUESTION BY CATEGORY GENERAL
           SqlDataAdapter sdaQuestionGeneral = new SqlDataAdapter("select Question.Content, Question.ID from ExamQuestion left join Question on ExamQuestion.QuestionID = Question.ID where Question.CategoryID = 1 and ExamQuestion.ExamID = " + Session["Exam"], sConfiguration.Configuration.Connection);
           DataTable tableQuestionGeneral = new DataTable();
           sdaQuestionGeneral.Fill(tableQuestionGeneral);
           lstView.DataSource = tableQuestionGeneral;
           lstView.DataBind();
       }

    }

    protected void ListItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            //determine if the row type is an Item
            DataRowView row = (DataRowView)e.Item.DataItem;
            if (row != null)
            {
                DbCommand cmd = GenericData.CreateCommand();
                cmd.CommandText = "SELECT * FROM Answer WHERE QuestionID = " + row["ID"];
                DataTable table = GenericData.ExecuteReader(cmd);

                RadioButtonList tmp = (RadioButtonList)e.Item.FindControl("lstQuestionView");
                if (tmp != null)
                {
                    tmp.DataTextField = "Content";
                    tmp.DataValueField = "ID";
                    tmp.DataSource = table;
                    tmp.DataBind();
                }
            }
        }
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        int Count = 0;
        for (int i = 0; i < lstView.Items.Count; i++)
        {
            if (lstView.Items[i].Controls.Count > 0)
            {
               
                ListViewDataItem dataItem = (ListViewDataItem)lstView.Items[i];
                HiddenField hfQuestion = (HiddenField)dataItem.FindControl("hfQuestionID");
                String QuestionID =  hfQuestion.Value.ToString();
                for (int j = 0; j < lstView.Items[i].Controls.Count; j++)
                {
                    if (lstView.Items[i].Controls[j] is RadioButtonList)
                    {
                        if (((RadioButtonList)lstView.Items[i].Controls[j]).SelectedItem != null)
                        {
                            String CandidateAnswer = ((RadioButtonList)lstView.Items[i].Controls[j]).SelectedItem.Value.ToString();
                            ViewState.Add(QuestionID, CandidateAnswer);
                            Count++;
                        }
                    }
                }
            }
        }

        if (Count < 5)
            Response.Write("<script>alert('You do not answer all the questions?');</script>");
        else
        {
            DataTable tableResult;
            int CountAnswerTrue = 0;
            //double CountMark = 0;
            //INSERT RESULT STEP1 IN TABLE CANDIDATEEXAMQUESITON
            DbCommand cmd = GenericData.CreateCommand();
            foreach (String item in ViewState.Keys)
            {
                cmd.CommandText = "INSERT INTO CandidateQuestionAnswer VALUES( " + Session["CandidateID"].ToString() + ", (SELECT ID FROM ExamQuestion WHERE QuestionID = " + item + " AND ExamID = " + Session["Exam"] + "), " + ViewState[item].ToString() + " )";
                //Response.Write("INSERT INTO CandidateQuestionAnswer VALUES( " + Session["CandidateID"].ToString() + ", (SELECT ID FROM ExamQuestion WHERE QuestionID = " + item + " AND ExamID = " + Session["Exam"] + "), " + ViewState[item].ToString() + " )" +"<br/>");
                GenericData.ExecuteNoneQuery(cmd);

                //get answer two
                cmd.CommandText = "select IsTrue From Answer where QuestionID = " + item + " and ID = " + ViewState[item].ToString();
                //Response.Write("select IsTrue From Answer where QuestionID = " + item + " and ID = " + ViewState[item].ToString() + "<br/>");

                tableResult = GenericData.ExecuteReader(cmd);
                if (tableResult.Rows.Count > 0)
                {
                    if ((bool)tableResult.Rows[0]["IsTrue"] == true)
                    {
                        CountAnswerTrue++;
                        //CountMark++;
                    }
                }

            }
            Session["CountAnserTrue"] = CountAnswerTrue.ToString();
            //Session["Mark"] = CountMark.ToString();

            //Response.Write("TotalMark : " + Session["Mark"].ToString() + "<br/>");
            //Response.Write("TotalAswerTrue : " + Session["CountAnserTrue"].ToString());

            Session.Add("Step1", "ok");
            Response.Redirect("Step2.aspx");
        }
    }
}